# makefile
# For guidelines see http://www.gnu.org/software/make/manual/make.html 
# For making F90 modules, see 
# http://www.ncsa.illinois.edu/UserInfo/Resources/Software/Intel/Compilers/8.1/f_ug1/fced_mod.htm

# DECLARE VARIABLES
# TODO: Use template rules with wildcards
# TODO: Reduce unnecessary recompilation of modules with %.mod : %.o rule
# from http://www.nicdan.id.au/computers/compiling/recompile.html

# File names
# execs      = executable files with no extension
# mods       = compiled module files with .mod extension
# (exec/mod)objects = object files with .o extention created by 
# compilation corresponding to executable/module files
# targets   = all files created by compilation
execs       = pisco   test
execobjects = pisco.o test.o
mods        = modarray.mod modparms.mod modfuncs.mod \
	modabund.mod modcoolcii.mod modcooloi.mod modcooltot.mod
modobjects  = modarray.o   modparms.o   modfuncs.o   \
	modabund.o   modcoolcii.o   modcooloi.o   modcooltot.o
targets     = $(execs) $(execobjects) $(mods) $(modobjects)

# Compile recipes
FC = mpif90
options = -fast -r8 -i4
%.o : %.F90
	$(FC) $(options) -c $<

# DEFINE MAKE RULES.
# Write rules in order from executable to most used prerequisite.
# List prerequisites in the following way:
# target : source_file most_independent_prerequisite_file ... most_dependent_prerequisite_file

# Link and compile executables
# for pisco
pisco : pisco.o $(modobjects)
	$(FC) -o $@ $^
pisco.o : pisco.F90 $(mods)
# for test
test : test.o $(modobjects)
	$(FC) -o $@ $^
test.o : test.F90 $(mods)

# Compile modules and objects
modarray.mod   : modarray.o
modarray.o     : modarray.F90
modparms.mod   : modparms.o
modparms.o     : modparms.F90
modfuncs.mod   : modfuncs.o     modparms.o
modfuncs.o     : modfuncs.F90   modparms.F90
modabund.mod   : modfuncs.o     modparms.o
modabund.o     : modfuncs.F90   modparms.F90
modcoolcii.mod : modcoolcii.o   modabund.o    modparms.o    modfuncs.o
modcoolcii.o   : modcoolcii.F90 modabund.F90  modparms.F90  modfuncs.F90
modcooloi.mod  : modcooloi.o    modabund.o    modparms.o    modfuncs.o
modcooloi.o    : modcooloi.F90  modabund.F90  modparms.F90  modfuncs.F90
modcooltot.mod : modcooltot.o   modcooloi.o   modcoolcii.o
modcooltot.o   : modcooltot.F90 modcooloi.F90 modcoolcii.F90

# Delete files made by compilation
.PHONY : clean
clean :
	rm $(targets)



## BELOW IS OLD
# disco : disco.o
# 	${FC} -profile=mpif90 $^ -o $@
# disco.o : disco.F90
# 	${FC} -profile=mpif90 ${opts} -c $^
# frisco:	frisco.o
# 	${FC} -f90=ifort $^ -o $@
# frisco.o:	frisco.F90
# 	${FC} ${opts} -c $^
# pcyg:	pcyg.o
# 	${FC} pcyg.o -o $@
# pcyg.o:	pcyg.F90
# 	${FC} ${opts} -80 -c $^
# bcyg:	bcyg.o
# 	${FC} bcyg.o -o $@
# bcyg.o:	bcyg.F90
# 	${FC} ${opts} -80 -c $^
# ploto:	ploto.o pltsub.o
# 	${FC} $^ -o $@ -L/opt/local/pgplot_5.2-x86_64 -lpgplot -L/usr/lib64/X11R6 -lX11
# ploto.o:	ploto.F90
# 	${FC} ${opts} -80 -c $^
# pltsub.o:	pltsub.F90
# 	${FC} ${opts} -80 -c $^
# excepts:	excepts.o
# 	${FC} $^ -o $@
# excepts.o:	excepts.F90
# 	${FC} ${opts} -80 -c $^